package com.ruoyi.system.handler;

import com.ruoyi.common.constant.TaskConstants;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.BigDecimalUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.TTaskInfo;
import com.ruoyi.system.domain.TUserInfo;
import com.ruoyi.system.domain.TUserMoneyChange;
import com.ruoyi.system.mapper.TUserInfoMapper;
import com.ruoyi.system.mapper.TUserMoneyChangeMapper;
import com.ruoyi.system.service.ITUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Objects;

@Component
public class TaskHandler {

    @Resource
    private ITUserInfoService userInfoService;
    @Autowired
    private RedisCache redisCache;
    @Resource
    private TUserMoneyChangeMapper moneyChangeMapper;
    @Resource
    private TUserInfoMapper userInfoMapper;
    private String money_fix = "koukuan";
    /**
     * 收益
     *
     * @param money
     * @param type 1-任务收入；2-佣金收入；3-体现支出；4-购买支出；5-充值明细;6-邀请关系，不参与计算;7-其他
     */
    public void shouyi(Long userId , BigDecimal money, Integer type, String msg) {
        TUserInfo userInfo = userInfoMapper.selectById(userId);
        Object redisCacheCacheObject = redisCache.getCacheObject(money_fix + userInfo.getId());
        if (Objects.nonNull(redisCacheCacheObject)) {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                shouyi(userId,money,type,msg);
                return;
            }
        }
        BigDecimal haveMoney = userInfo.getMoney();

        // 余额重组，扣除费用
        BigDecimal residueMoney = BigDecimalUtil.getValue(haveMoney).add(money);
        userInfo.setMoney(residueMoney);
        Integer integer = userInfoService.updateMoney(residueMoney, userInfo.getVersion(), userInfo.getId());
        if (integer < 1) {
            shouyi(userId, money, type,msg);
        }
        Date date = new Date();
        // 扣款成功继续向下操作 记录日志
        TUserMoneyChange tUserMoneyChange = new TUserMoneyChange();
        tUserMoneyChange.setType(type);
        tUserMoneyChange.setAuditStatus(2);
        tUserMoneyChange.setUserId(userInfo.getId());
        tUserMoneyChange.setBeforeMoney(haveMoney);
        tUserMoneyChange.setReason(msg);
        tUserMoneyChange.setMoney(money);
        tUserMoneyChange.setTime(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, date));
        tUserMoneyChange.setIsDelete(0);
        tUserMoneyChange.setCreateBy(userInfo.getId().toString());
        tUserMoneyChange.setCreateTime(date);
        tUserMoneyChange.setCreateTime(date);
        moneyChangeMapper.insert(tUserMoneyChange);
    }
    public void checkSubmitTask(TTaskInfo taskInfo) {
        // 确认任务状态
        Integer auditStatus = taskInfo.getAuditStatus();
        if (!TaskConstants.TASK_AUDIT_SUCCESS.equals(auditStatus)){
            throw new ServiceException("任务发布状态异常");
        }
        // 检查上下架状态
        Integer shelfStatus = taskInfo.getShelfStatus();
        if (!TaskConstants.TASK_SHELF_SUCCESS.equals(shelfStatus)){
            throw new ServiceException("任务已下架");
        }
        // 检测完成数量
        Integer taskNum = taskInfo.getTaskNum();
        Integer finishNum = taskInfo.getFinishNum();
        if (finishNum+1>taskNum){
            throw new ServiceException("完成任务数量已达上限，请下次加油。若有疑问请联系客服！");
        }
    }
}
